package com.sxfq.loansupermarket.backsystem.modular.logsf.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.logsf.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.logsf.service.ICustomerStatusLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/customerStatusLog")
public class CustomerStatusLogController extends BaseController {
    @Autowired
    ICustomerStatusLogService service;

    private String PREFIX = "/logsf/customerStatusLog/";

    /**
     * 修改预约日志
     */
    @RequestMapping(value = "")
    public String recharge(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "customer_reservations_update_log.html";
    }

    /**
     * 跳转病历号
     */
    @RequestMapping(value = "/caseNumber")
    public String caseNumber(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "customer_medical_number_log.html";
    }

    /**
     * 跳转到科室审核日志页面
     */
    @RequestMapping(value = "/treatment")
    public String treatment(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "treatment_unit_auditor_log.html";
    }

    /**
     * 跳转到病历操作日志页面
     */
    @RequestMapping(value = "/caseOperation")
    public String caseOperation(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "case_info_log.html";
    }

    /**
     * 跳转到影像操作日志页面
     */
    @RequestMapping(value = "/iconOperation")
    public String iconOperation(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "icon_info_log.html";
    }

    /**
     *
     */
    @RequestMapping(value = "/update")
    public String customerReservationsUpdate(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "old_customer_reservations_update_log.html";
    }

    /**
     * 黄页修改 跳转界面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/yellowUpdate")
    public String yellowUpdate(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "yellow_number_update_log.html";
    }

    /**
     *  取消预约患者
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(SearchCustomerReservationsUpdateLogInfo info, HttpServletRequest request) {
        Page<CustomerReservationsUpdateLogInfo> page = new PageFactory<CustomerReservationsUpdateLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            info.setLogtype("取消预约");
            page.setCurrent(0);
            List<CustomerReservationsUpdateLogInfo> list = service.getList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     *  病历号操作日志
     */
    @RequestMapping("/numberList")
    @ResponseBody
    public Object numberList(SearchCustomerReservationsUpdateLogInfo info, HttpServletRequest request) {
        Page<CustomerReservationsUpdateLogInfo> page = new PageFactory<CustomerReservationsUpdateLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<CustomerReservationsUpdateLogInfo> list = service.numberList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.numberList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryNumberCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     *
     */
    @RequestMapping("/updateList")
    @ResponseBody
    public Object updateList(SearchOldCustomerReservationsUpdateLogInfo info, HttpServletRequest request) {
        Page<OldCustomerReservationsUpdateLogInfo> page = new PageFactory<OldCustomerReservationsUpdateLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<OldCustomerReservationsUpdateLogInfo> list = service.getUpdateList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getUpdateList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesCountt(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     *
     */
    @RequestMapping("/getList")
    @ResponseBody
    public Object getList(SearchTreatmentUnitAuditorLogInfo info, HttpServletRequest request) {
        Page<TreatmentUnitAuditorLogInfo> page = new PageFactory<TreatmentUnitAuditorLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<TreatmentUnitAuditorLogInfo> list = service.getTreatmentList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getTreatmentList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getTreatmentCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     *病历操作日志
     */
    @RequestMapping("/getCaseList")
    @ResponseBody
    public Object getCaseList(SearchCaseInfoLogInfo info, HttpServletRequest request) {
        Page<CaseInfoLogInfo> page = new PageFactory<CaseInfoLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<CaseInfoLogInfo> list = service.getCaseLogList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getCaseLogList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getCaseInfoLogCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     *影像操作日志
     */
    @RequestMapping("/getIncoList")
    @ResponseBody
    public Object getIncoList(SearchCaseInfoLogInfo info, HttpServletRequest request) {
        Page<CaseInfoLogInfo> page = new PageFactory<CaseInfoLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<CaseInfoLogInfo> list = service.getIncoLogList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getCaseLogList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getIncoInfoLogCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    //查看病历日志
    @RequestMapping("/caseLogList/{caseId}")
    @ResponseBody
    public Object caseLogList(@PathVariable String caseId, SearchCaseInfoLogInfo info, HttpServletRequest request) {
        Page<CaseInfoLogInfo> page = new PageFactory<CaseInfoLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            info.setId(caseId);
            page.setCurrent(0);
            List<CaseInfoLogInfo> list = service.getCaseLogList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getCaseLogList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getCaseInfoLogCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    //查看病历日志
    @RequestMapping("/caseLogsList/{reservationsId}")
    @ResponseBody
    public Object caseLogsList(@PathVariable String reservationsId, SearchCaseInfoLogInfo info, HttpServletRequest request) {
        Page<CaseInfoLogInfo> page = new PageFactory<CaseInfoLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            info.setReservationsId(reservationsId);
            page.setCurrent(0);
            List<CaseInfoLogInfo> list = service.getCaseLogList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getCaseLogList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getCaseInfoLogCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     *
     */
    @RequestMapping("/yellowList")
    @ResponseBody
    public Object yellowList(SearchYellowNumberUpdateLogInfo info, HttpServletRequest request) {
        Page<YellowNumberUpdateLogInfo> page = new PageFactory<YellowNumberUpdateLogInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<YellowNumberUpdateLogInfo> list = service.getYellowList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getYellowList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesCounts(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }
}